package cn.kai.process.chain;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;

/**
 * 出入参数的责任链
 * Author: cKnight
 * Date: 2025/1/13
 */
public class FunctionChain<T> extends AbstractChain<Function<T,T>> {
    private T data;

    public FunctionChain(List<Function<T,T>> elements,T data){
        super(elements);
        this.data = data;
    }

    public static <T> FunctionChain<T> start(T data){
        return new FunctionChain(new ArrayList<>(),data);
    }

    public FunctionChain<T> next(Function<T,T> function){
        getElements().add(function);
        return this;
    }

    public T endT() {
        for (Function<T, T> element : getElements()) {
            data = element.apply(data);
        }
        return data;
    }


    @Override
    public void end() {
        for (Function<T, T> element : getElements()) {
            data = element.apply(data);
        }
    }
}
